﻿using durable.xml.ConfigProvider;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Durable.DControl
{
    public class FrameControl
    {
        XmlProvider xml = new XmlProvider();
        string configpath = string.Empty;      
        Dictionary<string, string> listConfig = new Dictionary<string, string>();
        public FrameControl()
        {
            configpath = Application.StartupPath + "/Config/Config.xml";
       
        }
       
        public void  LoadConfig(System.Windows.Forms.Control frame)
        {
            //configpath = path;
            xml._XmlAPIPath = configpath;

            listConfig = xml.Entity("9AA28130-EB3E-4D23-AACB-4C16326D9B3D");
            foreach (System.Windows.Forms.Control c in frame.Controls)
            {
              
                    Type t = c.GetType();
                    if (t.Name.ToLower().Equals("textbox"))
                    {
                        if (listConfig.ContainsKey(c.Tag.ToString()))
                        {
                            c.Text = listConfig[c.Tag.ToString()];
                        }

                    }
                    else if (t.Name.ToLower().Equals("combobox"))
                    {
                        ComboBox box = (ComboBox)c;
                        if (listConfig.ContainsKey(c.Tag.ToString()))
                        {
                            box.SelectedItem = listConfig[c.Tag.ToString()];
                        }
                    }
               
            }
        }
        public void SaveConfig(dynamic dy)
        {
            if (listConfig.Count == 0)
            {
                listConfig.Add("code", "9AA28130-EB3E-4D23-AACB-4C16326D9B3D");
                listConfig.Add("CompanyNo", "");
                listConfig.Add("MeterTabNo", "");
                listConfig.Add("Caliber", "");
                listConfig.Add("MeterType", "");
                listConfig.Add("Pump", "");
                listConfig.Add("Last_Count", "");
                listConfig.Add("Last_Counttimer", "");
                listConfig.Add("Set_Pressure1", "");
                listConfig.Add("Set_Pressure2", "");
                listConfig.Add("Server_Add", "");
                listConfig.Add("Set_QueShui_Time", "");
                listConfig.Add("Set_YiShui_Time", "");
                listConfig.Add("TcpToComputer_Add", "");
                listConfig.Add("Set_XuShui_Time", "");
                listConfig.Add("Server_Port", "");
                listConfig.Add("Set_Timer", "");
                listConfig.Add("Meter_Quantity", "");
            }

            foreach (KeyValuePair<string, string> item in dy)
            {
                if (listConfig.ContainsKey(item.Key))
                {
                    listConfig[item.Key] = item.Value;
                }
            }
            xml._XmlAPIPath =configpath;
            xml.Insert(listConfig);
        }
        public Action<object> OnRDCheckedChanged { get; set; }
        public Action<object> OnCBBSelectedIndexChanged { get; set; }
        public Action<object> OnTKeyUp { get; set; }
        public void LoadControl(System.Windows.Forms.Control frame, System.Windows.Forms.Control frame1)
        {
            foreach (Control c in frame.Controls)
            {
                Type t = c.GetType();
                if (t.Name.ToLower().Equals("textbox"))
                {
                    c.KeyUp += C_KeyUp;
                }
                else if (t.Name.ToLower().Equals("combobox"))
                {
                    ComboBox box = (ComboBox)c;
                    box.SelectedIndexChanged += Box_SelectedIndexChanged;

                }
            }
            foreach (Control c in frame1.Controls)
            {
                Type t = c.GetType();
                if (t.Name.ToLower().Equals("radiobutton"))
                {
                    RadioButton r = (RadioButton)c;
                    r.CheckedChanged += R_CheckedChanged;
                }
            }
        }

        private void R_CheckedChanged(object sender, EventArgs e)
        {
            if (OnRDCheckedChanged != null)
            {
                OnRDCheckedChanged(sender);
            }
        }

        private void Box_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox box = (ComboBox)sender;
            Dictionary<string, string> entity = new Dictionary<string, string>();
            entity.Add(box.Tag.ToString(), box.SelectedItem.ToString());
            SaveConfig(entity);
            if (OnCBBSelectedIndexChanged != null)
            {
                OnCBBSelectedIndexChanged(sender);
            }
        }

        private void C_KeyUp(object sender, KeyEventArgs e)
        {
            Control c = (Control)sender;
            Dictionary<string, string> entity = new Dictionary<string, string>();
            entity.Add(c.Tag.ToString(), c.Text);

            SaveConfig(entity);
            if (OnTKeyUp != null)
            {
                OnTKeyUp(sender);
            }
        }
    }
}
